<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2018 http://www.donglixia.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: 十万马 <962863675@qq.com>
// +----------------------------------------------------------------------
// | DateTime: 2018-02-09 16:17
// +----------------------------------------------------------------------

namespace app\admin_fussen\controller;

use app\admin_fussen\model\ArticleCat as ArticleCatModel;
use think\Db;
use think\Request;
use think\Session;

class ArticleCat extends Base
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->currentModel = new ArticleCatModel();//实例化当前模型
    }

    public function index()
    {
        //获取基础数据下拉列表
        $basicList = $this->currentModel->getBasicList(['cat_code'=>'article']);
        $this->assign('basicList', $basicList);
        return $this->fetch();
    }

    /**
     * 获取数据列表
     */
    public function getDataList()
    {
        $param = $this->request->param();

        if (!empty($param['cat_type'])) {
            $map['cat_type'] = $param['cat_type'];
        }

        if (!empty($param['cat_name'])) {
            $map['cat_name'] = ['like', '%'.$param['cat_name'].'%'];
        }

        $map['language'] = Session::get('config.language');
        $count = $this->currentModel->where($map)->count();
        $list = $this->currentModel->where($map)
            ->field('cat_id,pid,cat_name,cat_name as cat_name_text,cat_type,cat_type as cat_type_text,location,location as location_text')
            ->order('sort_num asc,cat_id desc')
            ->select();
        $list = \Tree::get_Table_tree($list, 'cat_name_text', 'cat_id');

        foreach ($list as $key=>$val) {
            unset($list[$key]['child']);
        }
        $list = array_slice($list, ($param['page'] - 1) * $param['limit'], $param['limit']);
        return ['code'=>0, 'msg'=>'', 'count'=>$count, 'data'=>$list];
    }

    public function edit()
    {
        $param = $this->request->param();
        if (!empty($param['cat_id'])) {
            $data = $this->currentModel->where('cat_id', $param['cat_id'])->find();
            if (empty($data)) {
                $this->error('信息不存在');
            }
            $data = $data->toArray();
            $this->assign('data', $data);
        }

        $map = [];
        if (!empty($param['cat_id'])) {
            $map['cat_type'] = Db::name('article_cat')->where('cat_id', $param['cat_id'])->value('cat_type');
        }
        $map['language'] = Session::get('config.language');
        $catList = $this->currentModel->where($map)->field('cat_id,pid,cat_name')->select();
        $catList = \Tree::get_Table_tree($catList, 'cat_name', 'cat_id');
        $this->assign('catList', $catList);

        //获取基础数据下拉列表
        $basicList = $this->currentModel->getBasicList(['cat_code'=>'article']);
        $this->assign('basicList', $basicList);
        return $this->fetch();
    }

    /**
     * 保存数据
     */
    public function save()
    {
        $param = $this->request->param();//获取请求数据
        //验证数据
        $result = $this->validate($param, 'ArticleCat');
        if ($result !== true) {
            $this->error($result);
        }

        try {
            //保存数据
            $this->currentModel->save($param);
        } catch (\Exception $e) {
            $msg = !empty($this->currentModel->getError()) ? $this->currentModel->getError() : $e->getMessage();
            save_error_log($msg.' ['.$e->getFile().':'.$e->getLine().']');
            $this->error($msg);
        }
        $this->success('保存成功！', 'edit?cat_id='.$this->currentModel->cat_id);
    }

    /**
     * 获取栏目列表
     * @param $id
     * @return string
     */
    public function getArticleCatList($id)
    {
        $catList = $this->currentModel->where('cat_id', '<>', $id)->field('cat_id,pid,cat_name')->select();
        return \Tree::get_Table_tree($catList, 'cat_name', 'cat_id');
    }

    /**
     * 根据pid 获取下拉列表，级联选择
     */
    public function getLinkSelect()
    {
        $param = $this->request->param();
        $pid = !empty($param['id']) ? $param['id'] : 0;
        $map['pid'] = $pid;
        $map['state'] = 1;//状态：0禁用，1启用

        if (!empty($param['cat_id'])) {
            $map['cat_id'] = ['<>', $param['cat_id']];
        }
        if (!empty($param['cat_type'])) {
            $map['cat_type'] = $param['cat_type'];
        }

        $data =  Db::name('article_cat')->where($map)->field('cat_id as id,cat_name as name')->order('sort_num asc, cat_id desc')->select();
        $this->success('获取成功', null, $data);
    }

}

